package wemai;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.internal.seleniumemulation.KeyEvent;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class m_126com extends WebMail {

	public void send(String uid, String password, String to, String subject, String content) throws Exception {

		try {
			String url = "http://www.126.com/";
			dr.get(url);

			dr.findElement(By.id("idInput")).sendKeys(uid);
			dr.findElement(By.id("pwdInput")).sendKeys(password);

			executeScript(" fSetStyle(10) "); //3.0

			dr.findElement(By.id("loginBtn")).click();

			waitWebElement(By.name("folder"));
			dr.switchTo().frame("folder");

			waitWebElement(By.linkText("写信")).click();

			dr.switchTo().defaultContent();
			waitWebElement(By.name("foldmain"));
			dr.switchTo().defaultContent().switchTo().frame("foldmain");

			waitWebElement(By.id("oSendButton2"));
			
			dr.findElement(By.id("sendSeparateHref")).click();
			
			
			content = StringUtils.remove(content, '\r');
			content = StringUtils.remove(content, '\n');

			content = StringEscapeUtils.escapeEcmaScript(content);

			
			//executeScript("  document.getElementById('to').value='" + to + "'   ");
			
			executeScript("  document.getElementById('subject').value='" + subject + "'   ");
			
			dr.switchTo().frame("editor").switchTo().frame("HtmlEditor");
			
			executeScript("  document.body.innerHTML='" + content + "'   ");
			
			dr.switchTo().defaultContent().switchTo().frame("foldmain");

			
			String sc="MM.compose.addAddress(\""+to+"\" );";
			
			executeScript(  sc  );
			

			dr.findElement(By.id("oSendButton2")).click();

			/*
			 * try { waitWebElement(By.className("fnt_Green"), 10); success(to);
			 * } catch (Exception e) { dr.switchTo().defaultContent(); String
			 * errmsg = dr.findElement(By.className("fnt_S12")).getText();
			 * System.err.println(errmsg); }
			 */

		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			sleep(6000);
			dr.quit();
		}
	}

	public static void main(String[] args) {
		try {
			String uid = "nml186";
			String password = "38451662";
			// String to = "charlesg47@163.com;nice2mu@qq.com";

			String to = "o21x@qq.com";
			String subject = "图片频道十分钟告";
			String content = 		   FileUtils.readFileToString(new File("c:\\csuu\\cont.txt"), "utf-8");


			new m_126com().send(uid, password, to, subject, content);
			
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
